{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:root:\n",
      "DeepCTR version 0.8.4 detected. Your version is 0.8.1.\n",
      "Use `pip install -U deepctr` to upgrade.Changelog: https://github.com/shenweichen/DeepCTR/releases/tag/v0.8.4\n"
     ]
    }
   ],
   "source": [
    "#导包\n",
    "import pandas as pd\n",
    "from sklearn.metrics import mean_squared_error\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from deepctr.models import WDL\n",
    "from deepctr.feature_column import SparseFeat,get_feature_names"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>userId</th>\n",
       "      <th>movieId</th>\n",
       "      <th>rating</th>\n",
       "      <th>timestamp</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1112486027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>29</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1112484676</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>32</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1112484819</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>47</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1112484727</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>50</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1112484580</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1048570</th>\n",
       "      <td>7120</td>\n",
       "      <td>168</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1175543061</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1048571</th>\n",
       "      <td>7120</td>\n",
       "      <td>253</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1175542225</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1048572</th>\n",
       "      <td>7120</td>\n",
       "      <td>260</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1175542035</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1048573</th>\n",
       "      <td>7120</td>\n",
       "      <td>261</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1175543376</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1048574</th>\n",
       "      <td>7120</td>\n",
       "      <td>266</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1175542454</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1048575 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         userId  movieId  rating   timestamp\n",
       "0             1        2     3.5  1112486027\n",
       "1             1       29     3.5  1112484676\n",
       "2             1       32     3.5  1112484819\n",
       "3             1       47     3.5  1112484727\n",
       "4             1       50     3.5  1112484580\n",
       "...         ...      ...     ...         ...\n",
       "1048570    7120      168     5.0  1175543061\n",
       "1048571    7120      253     4.0  1175542225\n",
       "1048572    7120      260     5.0  1175542035\n",
       "1048573    7120      261     4.0  1175543376\n",
       "1048574    7120      266     3.5  1175542454\n",
       "\n",
       "[1048575 rows x 4 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#读取数据\n",
    "data=pd.read_csv('ratings.csv')\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>userId</th>\n",
       "      <th>movieId</th>\n",
       "      <th>rating</th>\n",
       "      <th>timestamp</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.5</td>\n",
       "      <td>340880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>28</td>\n",
       "      <td>3.5</td>\n",
       "      <td>340785</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>31</td>\n",
       "      <td>3.5</td>\n",
       "      <td>340801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>46</td>\n",
       "      <td>3.5</td>\n",
       "      <td>340790</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>49</td>\n",
       "      <td>3.5</td>\n",
       "      <td>340774</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1048570</th>\n",
       "      <td>7119</td>\n",
       "      <td>163</td>\n",
       "      <td>5.0</td>\n",
       "      <td>494540</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1048571</th>\n",
       "      <td>7119</td>\n",
       "      <td>247</td>\n",
       "      <td>4.0</td>\n",
       "      <td>494524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1048572</th>\n",
       "      <td>7119</td>\n",
       "      <td>254</td>\n",
       "      <td>5.0</td>\n",
       "      <td>494519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1048573</th>\n",
       "      <td>7119</td>\n",
       "      <td>255</td>\n",
       "      <td>4.0</td>\n",
       "      <td>494545</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1048574</th>\n",
       "      <td>7119</td>\n",
       "      <td>260</td>\n",
       "      <td>3.5</td>\n",
       "      <td>494530</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1048575 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         userId  movieId  rating  timestamp\n",
       "0             0        1     3.5     340880\n",
       "1             0       28     3.5     340785\n",
       "2             0       31     3.5     340801\n",
       "3             0       46     3.5     340790\n",
       "4             0       49     3.5     340774\n",
       "...         ...      ...     ...        ...\n",
       "1048570    7119      163     5.0     494540\n",
       "1048571    7119      247     4.0     494524\n",
       "1048572    7119      254     5.0     494519\n",
       "1048573    7119      255     4.0     494545\n",
       "1048574    7119      260     3.5     494530\n",
       "\n",
       "[1048575 rows x 4 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sparse_features = [\"movieId\", \"userId\", \"timestamp\"]#准备特征\n",
    "target = ['rating']#准备标签\n",
    "#特征数值化 data里面3个特征都用LabelEncoder处理一下 就是把特征里面的值 从0到n开始编号\n",
    "for f in  sparse_features:\n",
    "    transfor = LabelEncoder()\n",
    "    data[f] = transfor.fit_transform(data[f])\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/initializers.py:143: calling RandomNormal.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Call initializer instance with the dtype argument instead of passing it to the constructor\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/initializers.py:143: calling RandomNormal.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Call initializer instance with the dtype argument instead of passing it to the constructor\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[SparseFeat(name='movieId', vocabulary_size=14026, embedding_dim=4, use_hash=False, dtype='int32', embeddings_initializer=<tensorflow.python.keras.initializers.RandomNormal object at 0x7f5af8408a50>, embedding_name='movieId', group_name='default_group', trainable=True),\n",
       " SparseFeat(name='userId', vocabulary_size=7120, embedding_dim=4, use_hash=False, dtype='int32', embeddings_initializer=<tensorflow.python.keras.initializers.RandomNormal object at 0x7f5af3312a50>, embedding_name='userId', group_name='default_group', trainable=True),\n",
       " SparseFeat(name='timestamp', vocabulary_size=822889, embedding_dim=4, use_hash=False, dtype='int32', embeddings_initializer=<tensorflow.python.keras.initializers.RandomNormal object at 0x7f5af3233cd0>, embedding_name='timestamp', group_name='default_group', trainable=True)]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#生成词向量  计算每个特征中的 不同特征值的个数\n",
    "fixlen_feature_columns = [SparseFeat(feat, vocabulary_size=data[feat].nunique()) for feat in sparse_features]\n",
    "fixlen_feature_columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "<class 'deepctr.feature_column.SparseFeat'> \n",
      " SparseFeat(name='movieId', vocabulary_size=14026, embedding_dim=4, use_hash=False, dtype='int32', embeddings_initializer=<tensorflow.python.keras.initializers.RandomNormal object at 0x7f5af8408a50>, embedding_name='movieId', group_name='default_group', trainable=True) \n",
      "\n",
      "<class 'deepctr.feature_column.SparseFeat'> \n",
      " SparseFeat(name='userId', vocabulary_size=7120, embedding_dim=4, use_hash=False, dtype='int32', embeddings_initializer=<tensorflow.python.keras.initializers.RandomNormal object at 0x7f5af3312a50>, embedding_name='userId', group_name='default_group', trainable=True) \n",
      "\n",
      "<class 'deepctr.feature_column.SparseFeat'> \n",
      " SparseFeat(name='timestamp', vocabulary_size=822889, embedding_dim=4, use_hash=False, dtype='int32', embeddings_initializer=<tensorflow.python.keras.initializers.RandomNormal object at 0x7f5af3233cd0>, embedding_name='timestamp', group_name='default_group', trainable=True) \n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(type(fixlen_feature_columns))#fixlen_feature_columns是个list类型  里面的元素是deepctr.feature_column.SparseFeat类型\n",
    "for i in fixlen_feature_columns:\n",
    "    print(type(i),'\\n',i,'\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['movieId', 'userId', 'timestamp'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "linear_feature_columns = fixlen_feature_columns\n",
    "dnn_feature_columns = fixlen_feature_columns\n",
    "feature_names = get_feature_names(linear_feature_columns + dnn_feature_columns)\n",
    "print(feature_names,type(feature_names))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 划分数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>userId</th>\n",
       "      <th>movieId</th>\n",
       "      <th>rating</th>\n",
       "      <th>timestamp</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>691206</th>\n",
       "      <td>4586</td>\n",
       "      <td>203</td>\n",
       "      <td>2.5</td>\n",
       "      <td>414830</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>304310</th>\n",
       "      <td>2080</td>\n",
       "      <td>1192</td>\n",
       "      <td>4.0</td>\n",
       "      <td>396635</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>921199</th>\n",
       "      <td>6149</td>\n",
       "      <td>1957</td>\n",
       "      <td>4.0</td>\n",
       "      <td>163364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30779</th>\n",
       "      <td>244</td>\n",
       "      <td>1664</td>\n",
       "      <td>5.0</td>\n",
       "      <td>72801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>131579</th>\n",
       "      <td>898</td>\n",
       "      <td>574</td>\n",
       "      <td>3.0</td>\n",
       "      <td>10548</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>537565</th>\n",
       "      <td>3624</td>\n",
       "      <td>6777</td>\n",
       "      <td>3.0</td>\n",
       "      <td>674899</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>694087</th>\n",
       "      <td>4599</td>\n",
       "      <td>5</td>\n",
       "      <td>3.5</td>\n",
       "      <td>512413</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>790151</th>\n",
       "      <td>5270</td>\n",
       "      <td>1002</td>\n",
       "      <td>2.0</td>\n",
       "      <td>54140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>139845</th>\n",
       "      <td>939</td>\n",
       "      <td>6440</td>\n",
       "      <td>3.5</td>\n",
       "      <td>346366</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>283437</th>\n",
       "      <td>1950</td>\n",
       "      <td>1577</td>\n",
       "      <td>4.0</td>\n",
       "      <td>333743</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>838860 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        userId  movieId  rating  timestamp\n",
       "691206    4586      203     2.5     414830\n",
       "304310    2080     1192     4.0     396635\n",
       "921199    6149     1957     4.0     163364\n",
       "30779      244     1664     5.0      72801\n",
       "131579     898      574     3.0      10548\n",
       "...        ...      ...     ...        ...\n",
       "537565    3624     6777     3.0     674899\n",
       "694087    4599        5     3.5     512413\n",
       "790151    5270     1002     2.0      54140\n",
       "139845     939     6440     3.5     346366\n",
       "283437    1950     1577     4.0     333743\n",
       "\n",
       "[838860 rows x 4 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将数据集切分成训练集和测试集\n",
    "train, test = train_test_split(data, test_size=0.2)\n",
    "train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>userId</th>\n",
       "      <th>movieId</th>\n",
       "      <th>rating</th>\n",
       "      <th>timestamp</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>177324</th>\n",
       "      <td>1199</td>\n",
       "      <td>529</td>\n",
       "      <td>3.0</td>\n",
       "      <td>359313</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>105148</th>\n",
       "      <td>734</td>\n",
       "      <td>1195</td>\n",
       "      <td>3.0</td>\n",
       "      <td>532592</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>763809</th>\n",
       "      <td>5083</td>\n",
       "      <td>2258</td>\n",
       "      <td>2.0</td>\n",
       "      <td>128044</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>555288</th>\n",
       "      <td>3730</td>\n",
       "      <td>2088</td>\n",
       "      <td>4.5</td>\n",
       "      <td>650666</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>368169</th>\n",
       "      <td>2492</td>\n",
       "      <td>353</td>\n",
       "      <td>3.0</td>\n",
       "      <td>35667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>791691</th>\n",
       "      <td>5283</td>\n",
       "      <td>6002</td>\n",
       "      <td>4.0</td>\n",
       "      <td>557096</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>801519</th>\n",
       "      <td>5347</td>\n",
       "      <td>6220</td>\n",
       "      <td>3.0</td>\n",
       "      <td>561683</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1041517</th>\n",
       "      <td>7055</td>\n",
       "      <td>1495</td>\n",
       "      <td>2.0</td>\n",
       "      <td>398122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>352122</th>\n",
       "      <td>2373</td>\n",
       "      <td>1208</td>\n",
       "      <td>2.0</td>\n",
       "      <td>160571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>640061</th>\n",
       "      <td>4280</td>\n",
       "      <td>1143</td>\n",
       "      <td>4.0</td>\n",
       "      <td>817300</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>209715 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         userId  movieId  rating  timestamp\n",
       "177324     1199      529     3.0     359313\n",
       "105148      734     1195     3.0     532592\n",
       "763809     5083     2258     2.0     128044\n",
       "555288     3730     2088     4.5     650666\n",
       "368169     2492      353     3.0      35667\n",
       "...         ...      ...     ...        ...\n",
       "791691     5283     6002     4.0     557096\n",
       "801519     5347     6220     3.0     561683\n",
       "1041517    7055     1495     2.0     398122\n",
       "352122     2373     1208     2.0     160571\n",
       "640061     4280     1143     4.0     817300\n",
       "\n",
       "[209715 rows x 4 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(838860, 4) (209715, 4)\n"
     ]
    }
   ],
   "source": [
    "print(train.shape,test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'movieId': array([ 203, 1192, 1957, ..., 1002, 6440, 1577]),\n",
       " 'userId': array([4586, 2080, 6149, ..., 5270,  939, 1950]),\n",
       " 'timestamp': array([414830, 396635, 163364, ...,  54140, 346366, 333743])}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_model_input = {name:train[name].values for name in feature_names}\n",
    "train_model_input"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'movieId': array([ 529, 1195, 2258, ..., 1495, 1208, 1143]),\n",
       " 'userId': array([1199,  734, 5083, ..., 7055, 2373, 4280]),\n",
       " 'timestamp': array([359313, 532592, 128044, ..., 398122, 160571, 817300])}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_model_input = {name:test[name].values for name in feature_names}\n",
    "test_model_input"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 模型训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/indexed_slices.py:434: UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.\n",
      "  \"Converting sparse IndexedSlices to a dense Tensor of unknown shape. \"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2622/2622 [==============================] - 76s 29ms/step - loss: 0.9438 - mse: 0.9373 - val_loss: 0.7556 - val_mse: 0.7423\n"
     ]
    }
   ],
   "source": [
    "# 使用WDL进行训练\n",
    "model = WDL(linear_feature_columns, dnn_feature_columns, task='regression')\n",
    "model.compile(\"adam\", \"mse\", metrics=['mse'], )\n",
    "history = model.fit(train_model_input, train[target].values, batch_size=256, epochs=1, verbose=True, validation_split=0.2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 模型效果评估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "test RMSE 0.8615683373940805\n"
     ]
    }
   ],
   "source": [
    "# 使用WDL进行预测\n",
    "pred_ans = model.predict(test_model_input, batch_size=256)\n",
    "# 输出RMSE或MSE\n",
    "mse = round(mean_squared_error(test[target].values, pred_ans), 4)\n",
    "rmse = mse ** 0.5\n",
    "print(\"test RMSE\", rmse)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
